using System;
using System.Web.UI;
using Nethouse.Lib.Resolver;

namespace Nethouse.Lib.Mvp
{
    public abstract class MvpPage<TView> : Page, IView where TView : IView
    {
        private readonly IPresenter<TView> presenter;

        protected MvpPage()
        {
            presenter = ResolverFactory.Resolver.Resolve<IPresenter<TView>>();
        }

        protected override void OnInit(EventArgs e)
        {
            bool triggerViewCreated = (!IsPostBack);
            presenter.MvpInit(this, triggerViewCreated);

            base.OnInit(e);
        }

        protected void ActivateEvent(MvpEventHandler eventToActivate)
        {
            if (eventToActivate != null)
            {
                eventToActivate();
            }
        }

        protected void ActivateEvent<TType>(MvpEventHandler<TType> eventToActivate, TType param) 
        {
            if (eventToActivate != null)
            {
                eventToActivate(param);
            }
        }
    }
}